const express = require('express');
const router = express.Router();
const bcrypt = require('bcrypt');
var jwt = require('jsonwebtoken');


/* GET users listing. */
const User = require('../models/Admin');
const Ju = require("../models/Jubao")
//const passport = require('passport');


router.post('/zhuce', (req, res) => {
    User.findOne({ zhanghao: req.body.zhanghao })
        .then((zhanghao) => {
            if (zhanghao) {
                return res.status(400).json({ msg: '该账号已被注册' })
            } else {
                var newUser = new User({
                    zhanghao: req.body.zhanghao,
                    username: req.body.username,
                    password: req.body.password,
                    state: req.body.state
                })

                bcrypt.genSalt(10, function (err, salt) {
                    bcrypt.hash(newUser.password, salt, (err, hash) => {
                        if (err) throw err;
                        newUser.password = hash;
                        newUser.save()
                            .then(user => res.json(user))
                            .catch(err => console.log(err));
                    });
                });
            }
        });
});

router.post('/login', (req, res) => {
    const zhanghao = req.body.zhanghao;
    const password = req.body.password;
    User.findOne({ zhanghao })
        .then(user => {
            if (!user) {
                return res.json({ msg: '账号不存在' })
            }
            bcrypt.compare(password, user.password)
                .then(isMatch => {
                    if (isMatch) {
                        // jwt.sign("规则"，加密名字，过期时间(s)，箭头函数)
                        const rule = {
                            id: user.id,
                            zhanghao: user.zhanghao,
                        }
                        jwt.sign(rule, "thek", { expiresIn: 72000 }, (err, token) => {
                            if (err) throw err;
                            res.json({
                                data: user,
                                status: 200,
                                success: true,
                                token: 'Bearer ' + token
                            })
                        })
                    } else {
                        res.json({
                            status: 400,
                            success: false,
                            msg: '用户信息错误'
                        })
                    }
                })
        })
})

// router.get("/路径","验证token",(req,res)=>{})

// router.get("/soucang", passport.authenticate("jwt", { session: false }), (req, res) => {
//     res.json({
//         id: req.user.id,
//         zhanghao: req.user.zhanghao,
//         username: req.user.username,
//         state: req.user.state
//     })
// })

router.get('/getJu', (req, res) => {
    Ju.find({ state: 0 })
        .then(ju => res.send({ju:ju,status:200}))
})

router.post('/upJu/:id', (req, res) => {
    Ju.updateOne({ _id: req.params.id }, { $set: { state: 1 }})
        .then(res.sendStatus(200))
})

module.exports = router;
